<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  iphone
 */

/**
 * Backoffice Ajax login action
 */
class m_iphone_actions_IphoneLoginAction extends f_core_Action
{
	public function execute()
	{
        $iphoneService = m_iphone_services_IphoneService::getInstance();

		// We get the name and the password
		$login = $this->getRequestParameter('login');
		$password = $this->getRequestParameter('password');

        $key = m_backoffice_services_BackOfficeService::getKey();

		// get the user from db
		$user = f_document_Provider::getInstance()
						->loadDocuments("modules_users/user")
						->whereIsEqual("login", $login)
						->whereIsEqual("password", md5($password.$key))
						->retrieveFirstDocument();

        if($user instanceof f_document_baseDocument)
        {
            $rights = m_rights_services_RightsService::getInstance()->getUserRights($user);
            $this->getUserContext()->removeRights()->setUser($user);

            foreach($rights as $right)
            {
                $this->getUserContext()->addRight($right);
            }
        }
        else
        {
            $this->getUserContext()->removeUser();
        }

        $out = $iphoneService->login();
        echo $out;
	}
	
	public function validate()
	{
        return true;
	}

	public function errorHandler()
	{
        return null;
	}	
}